//
//  NSString+Format.m
//  STTools
//
//  Created by stoneobs on 16/10/8.
//  Copyright © 2016年 stoneobs. All rights reserved.
//

#import "NSString+STTools_Formatter.h"

@implementation NSString (STTools_Formatter)



- (BOOL)stoneobs_fun_st_isChinese
{
    NSString *stoneobs_temp_match = @"(^[\u4e00-\u9fa5]+$)";
    NSPredicate *stoneobs_temp_predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", stoneobs_temp_match];
    return [stoneobs_temp_predicate evaluateWithObject:self];
}

- (BOOL)stoneobs_fun_st_isPureNumandCharacters
{
    NSString *  stoneobs_temp_string = [self stringByTrimmingCharactersInSet:[NSCharacterSet decimalDigitCharacterSet]];
    if(stoneobs_temp_string.length > 0)
    {
        return NO;
    }
    return YES;
}
- (CGFloat)stoneobs_fun_st_heigthWithwidth:(CGFloat)width font:(CGFloat)font
{
    
    NSString * stoneobs_temp_str =self ;
    CGRect stoneobs_temp_rect = [stoneobs_temp_str boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]} context:nil];
    
    return stoneobs_temp_rect.size.height;
}
- (CGFloat)stoneobs_fun_st_widthWithheight:(CGFloat)height font:(CGFloat)font
{
    NSString * stoneobs_temp_str =self ;
    CGRect stoneobs_temp_rect = [stoneobs_temp_str boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, height) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]} context:nil];
    return stoneobs_temp_rect.size.width;
}
+ (void)stoneobs_fun_lableAutoAdjustWitdhWithLabel:(UILabel *)lable{
    CGFloat stoneobs_temp_witdh = [lable.text stoneobs_fun_st_widthWithheight:lable.frame.size.height font:lable.font.pointSize];
    lable.width = stoneobs_temp_witdh;
}
+ (void)stoneobs_fun_lableAutoAdjustheightWithLabel:(UILabel *)lable{
    lable.numberOfLines = 0;
    CGFloat stoneobs_temp_height = [lable.text stoneobs_fun_st_heigthWithwidth:lable.frame.size.width font:lable.font.pointSize];
    lable.height = stoneobs_temp_height;
}
- (NSString *)stoneobs_fun_st_disable_emojiSrting
{
    NSString * stoneobs_temp_text = self;
    NSRegularExpression *stoneobs_temp_regex = [NSRegularExpression regularExpressionWithPattern:@"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]" options:NSRegularExpressionCaseInsensitive error:nil];
    NSString *stoneobs_temp_modifiedString = [stoneobs_temp_regex stringByReplacingMatchesInString:stoneobs_temp_text
                                                               options:0
                                                                 range:NSMakeRange(0, [stoneobs_temp_text length])
                                                          withTemplate:@""];
    return stoneobs_temp_modifiedString;
}
//安全手机号
- (NSString*)stoneobs_fun_st_safetyPhone
{
    if (self.length != 11) {
        // NSLog(@"电话号码不是11位,修改失败");
        return self;
    }
    NSString * stoneobs_temp_beginStr = [self substringToIndex:3];
    NSString * stoneobs_temp_lastStr = [self substringFromIndex:7];
    return  [NSString stringWithFormat:@"%@****%@",stoneobs_temp_beginStr,stoneobs_temp_lastStr];
}
//安全邮箱 56****@qq.com
- (NSString *)stoneobs_fun_st_safetyEmail
{
    NSArray * stoneobs_temp_array = [self componentsSeparatedByString:@"@"];
    if (stoneobs_temp_array.count != 2) {
        //NSLog(@"邮箱格式错误");
        return self;
    }
    NSString * stoneobs_temp_begin = stoneobs_temp_array.firstObject;
    NSString * stoneobs_temp_first = [stoneobs_temp_begin substringToIndex:2];
    
    NSString * stoneobs_temp_headStr = [NSString stringWithFormat:@"%@****",stoneobs_temp_first];
    NSString * stoneobs_temp_footStr = stoneobs_temp_array.lastObject;
    
    NSString * stoneobs_temp_finsh = [[NSString stringWithFormat:@"%@@%@",stoneobs_temp_headStr ,stoneobs_temp_footStr ] lowercaseString];
    
    return stoneobs_temp_finsh;
}

// 判断邮箱格式
- (BOOL)stoneobs_fun_st_isValidateEmail;
{
    NSString * stoneobs_temp_email = self;
    if (!stoneobs_temp_email) {
        return NO;
    }
    // [A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}
    NSString *stoneobs_temp_emailRegex = @"^[0-9a-zA-Z][0-9a-zA-Z_.-]{0,31}@([0-9a-zA-Z][0-9a-zA-Z-]{0,30}[0-9A-Za-z]\\.){1,4}[a-zA-Z]{2,4}$";
    NSPredicate *stoneobs_temp_emailPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", stoneobs_temp_emailRegex];
    return [stoneobs_temp_emailPredicate evaluateWithObject:stoneobs_temp_email];
}

// 判断是否为电话号码
- (BOOL)stoneobs_fun_st_isValidatePhoneNumber;
{
    NSString * stoneobs_temp_phoneNumber = self;
    if (stoneobs_temp_phoneNumber.length != 11)
    {
        return NO;
    }
    /**
     * 手机号码:
     * 13[0-9], 14[5,7], 15[0, 1, 2, 3, 5, 6, 7, 8, 9], 17[0, 1, 6, 7, 8], 18[0-9]
     * 移动号段: 134,135,136,137,138,139,147,150,151,152,157,158,159,170,178,182,183,184,187,188
     * 联通号段: 130,131,132,145,152,155,156,170,171,176,185,186
     * 电信号段: 133,134,153,170,177,180,181,189
     */
    NSString *stoneobs_temp_MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|7[01678]|8[0-9])\\d{8}$";
    /**
     * 中国移动：China Mobile
     * 134,135,136,137,138,139,147,150,151,152,157,158,159,170,178,182,183,184,187,188
     */
    NSString *stoneobs_temp_CM = @"^1(3[4-9]|4[7]|5[0-27-9]|7[08]|8[2-478])\\d{8}$";
    /**
     * 中国联通：China Unicom
     * 130,131,132,145,152,155,156,170,171,176,185,186
     */
    NSString *stoneobs_temp_CU = @"^1(3[0-2]|4[5]|5[256]|7[016]|8[56])\\d{8}$";
    /**
     * 中国电信：China Telecom
     * 133,134,153,170,177,180,181,189
     */
    NSString *stoneobs_temp_CT = @"^1(3[34]|53|7[07]|8[019])\\d{8}$";
    
    NSPredicate *stoneobs_temp_regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", stoneobs_temp_MOBILE];
    NSPredicate *stoneobs_temp_regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", stoneobs_temp_CM];
    NSPredicate *stoneobs_temp_regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", stoneobs_temp_CU];
    NSPredicate *stoneobs_temp_regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", stoneobs_temp_CT];
    
    if (([stoneobs_temp_regextestmobile evaluateWithObject:stoneobs_temp_phoneNumber] == YES)
        || ([stoneobs_temp_regextestcm evaluateWithObject:stoneobs_temp_phoneNumber] == YES)
        || ([stoneobs_temp_regextestct evaluateWithObject:stoneobs_temp_phoneNumber] == YES)
        || ([stoneobs_temp_regextestcu evaluateWithObject:stoneobs_temp_phoneNumber] == YES))
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

// 判断是否3~20为电话号码
- (BOOL)stoneobs_fun_st_isValidateTelephoneNumber
{
    NSString * stoneobs_temp_phoneNumber = self;
    NSString *stoneobs_temp_MOBILE = @"[0-9]{3,20}$";
    NSPredicate *stoneobs_temp_regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", stoneobs_temp_MOBILE];
    if ([stoneobs_temp_regextestmobile evaluateWithObject:stoneobs_temp_phoneNumber] == YES) {
        return  YES;
    } else {
        return NO;
    }
}
- (BOOL)stoneobs_fun_st_isValidatePwd{
    NSString * stoneobs_temp_email = self;
    if (!stoneobs_temp_email.length) {
        return NO;
    }
    // [A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}
    NSString *stoneobs_temp_pattern = @"^(?![0-9]+$)(?![a-zA-Z]+$)[a-zA-Z0-9]{6,16}";
    NSPredicate *stoneobs_temp_pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", stoneobs_temp_pattern];
    BOOL stoneobs_temp_isMatch = [stoneobs_temp_pred evaluateWithObject:stoneobs_temp_email];
    return stoneobs_temp_isMatch;
    
    
}
// 判断url格式
- (BOOL)stoneobs_fun_st_isValidateUrl
{
    NSString * stoneobs_temp_url = self;
    // [A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}
    static NSString *stoneobs_temp_urlRegex = @"((http[s]{0,1}|ftp)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|(www.[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)";
    NSPredicate *stoneobs_temp_urlPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", stoneobs_temp_urlRegex];
    return [stoneobs_temp_urlPredicate evaluateWithObject:stoneobs_temp_url];
}

- (BOOL)stoneobs_fun_st_st_isBlank
{
    NSString * stoneobs_temp_string = self;
    if (stoneobs_temp_string == nil || stoneobs_temp_string == NULL) {
        return YES;
    }
    if ([stoneobs_temp_string isKindOfClass:[NSNull class]]) {
        return YES;
    }
    if ([[stoneobs_temp_string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
        return YES;
    }
    return NO;
}

- (NSAttributedString *)stoneobs_fun_st_convertAttributeStringWithKeyWord:(NSString *)keyword
                                                  attributes:(NSDictionary *)optins{
    if (!keyword.length) {
        keyword = @"" ;
    }
    keyword = [keyword stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    
    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:self];
    
    NSRange range;
    if ((range = [[self uppercaseString] rangeOfString:[keyword uppercaseString] ]).location != NSNotFound) {
        [attributeString addAttributes:optins range:range];
        return attributeString;
        
    }else if((range = [[[self st_getPinYinLetter] uppercaseString] rangeOfString:[keyword uppercaseString]]).location != NSNotFound){
        [attributeString addAttributes:optins range:range];
        return attributeString;
    }else{
        return attributeString;
    }
    
}

- (NSAttributedString *)stoneobs_fun_st_convertAttributeStringWithKeyWords:(NSArray *)keywords
                                                   attributes:(NSArray<NSDictionary *> *)options{
    NSAssert(keywords.count == options.count, @"属性与关键字的数量保持一致");
    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:self];
    
    for (int i = 0; i < keywords.count; i ++) {
        @autoreleasepool {
            NSString *keyword = keywords[i];
            NSRange range = [self rangeOfString:keyword];
            if (range.location != NSNotFound) {
                NSDictionary *option = options[i];
                [attributeString addAttributes:option range:range];
            }
        }
    }
    return attributeString;
}
- (BOOL)stoneobs_fun_st_isIncludeSpecialCharacters{
    
    NSString * stoneobs_temp_content = self;
    NSString *stoneobs_temp_str =@"^[A-Za-z0-9\\u4e00-\u9fa5]+$";
    NSPredicate* stoneobs_temp_emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", stoneobs_temp_str];
    if (![stoneobs_temp_emailTest evaluateWithObject:stoneobs_temp_content]) {
        return YES;
    }
    return NO;
}
//获取首字母
-(NSString *)st_getPinYinLetter{
    NSMutableString *stoneobs_temp_temp = [[NSMutableString alloc]initWithString:@""];
    for(int i =0; i < [self length]; i++)
    {
        NSMutableString *ms = [[NSMutableString alloc] initWithString:[self substringWithRange:NSMakeRange(i, 1)]];
        CFStringTransform((__bridge CFMutableStringRef)ms, 0, kCFStringTransformMandarinLatin, NO);
        CFStringTransform((__bridge CFMutableStringRef)ms, 0, kCFStringTransformStripDiacritics, NO);
        if (ms.length<1)continue;
        [stoneobs_temp_temp appendString:[[ms substringToIndex:1] uppercaseString]];
    }
    return stoneobs_temp_temp;
}

- (BOOL)stoneobs_fun_st_verifyIDCardNumber{
    NSString * stoneobs_temp_IDCardNumber = self;
    stoneobs_temp_IDCardNumber = [stoneobs_temp_IDCardNumber stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if ([stoneobs_temp_IDCardNumber length] != 18) {
        return NO;
        
    }
    NSString *stoneobs_temp_mmdd = @"(((0[13578]|1[02])(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)(0[1-9]|[12][0-9]|30))|(02(0[1-9]|[1][0-9]|2[0-8])))";
    NSString *stoneobs_temp_leapMmdd = @"0229";
    NSString *stoneobs_temp_year = @"(19|20)[0-9]{2}";
    NSString *stoneobs_temp_leapYear = @"(19|20)(0[48]|[2468][048]|[13579][26])";
    NSString *stoneobs_temp_yearMmdd = [NSString stringWithFormat:@"%@%@", stoneobs_temp_year, stoneobs_temp_mmdd];
    NSString *stoneobs_temp_leapyearMmdd = [NSString stringWithFormat:@"%@%@", stoneobs_temp_leapYear, stoneobs_temp_leapMmdd];
    NSString *stoneobs_temp_yyyyMmdd = [NSString stringWithFormat:@"((%@)|(%@)|(%@))", stoneobs_temp_yearMmdd, stoneobs_temp_leapyearMmdd, @"20000229"];
    NSString *stoneobs_temp_area = @"(1[1-5]|2[1-3]|3[1-7]|4[1-6]|5[0-4]|6[1-5]|82|[7-9]1)[0-9]{4}";
    NSString *stoneobs_temp_regex = [NSString stringWithFormat:@"%@%@%@", stoneobs_temp_area, stoneobs_temp_yyyyMmdd , @"[0-9]{3}[0-9Xx]"];
    NSPredicate *stoneobs_temp_regexTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", stoneobs_temp_regex];
    if (![stoneobs_temp_regexTest evaluateWithObject:stoneobs_temp_IDCardNumber]) {
        return NO;
        
    }
    int summary = ([stoneobs_temp_IDCardNumber substringWithRange:NSMakeRange(0,1)].intValue + [stoneobs_temp_IDCardNumber substringWithRange:NSMakeRange(10,1)].intValue) *7 + ([stoneobs_temp_IDCardNumber substringWithRange:NSMakeRange(1,1)].intValue + [stoneobs_temp_IDCardNumber substringWithRange:NSMakeRange(11,1)].intValue) *9 + ([stoneobs_temp_IDCardNumber substringWithRange:NSMakeRange(2,1)].intValue + [stoneobs_temp_IDCardNumber substringWithRange:NSMakeRange(12,1)].intValue) *10 + ([stoneobs_temp_IDCardNumber substringWithRange:NSMakeRange(3,1)].intValue + [stoneobs_temp_IDCardNumber substringWithRange:NSMakeRange(13,1)].intValue) *5 + ([stoneobs_temp_IDCardNumber substringWithRange:NSMakeRange(4,1)].intValue + [stoneobs_temp_IDCardNumber substringWithRange:NSMakeRange(14,1)].intValue) *8 + ([stoneobs_temp_IDCardNumber substringWithRange:NSMakeRange(5,1)].intValue + [stoneobs_temp_IDCardNumber substringWithRange:NSMakeRange(15,1)].intValue) *4 + ([stoneobs_temp_IDCardNumber substringWithRange:NSMakeRange(6,1)].intValue + [stoneobs_temp_IDCardNumber substringWithRange:NSMakeRange(16,1)].intValue) *2 + [stoneobs_temp_IDCardNumber substringWithRange:NSMakeRange(7,1)].intValue *1 + [stoneobs_temp_IDCardNumber substringWithRange:NSMakeRange(8,1)].intValue *6 + [stoneobs_temp_IDCardNumber substringWithRange:NSMakeRange(9,1)].intValue *3;
    NSInteger remainder = summary % 11;
    NSString *checkBit = @"";
    NSString *checkString = @"10X98765432";
    checkBit = [checkString substringWithRange:NSMakeRange(remainder,1)];// 判断校验位
    return [checkBit isEqualToString:[[stoneobs_temp_IDCardNumber substringWithRange:NSMakeRange(17,1)] uppercaseString]];
    
}
- (NSURL *)stoneobs_fun_st_safeImageUrl{
    NSString * deal = self.stringByURLDecode;
    return [NSURL URLWithString:deal];
}
//2位小数
- (NSString*)stoneobs_fun_st_2float{
    return [NSString stringWithFormat:@"%0.2f",self.floatValue];
}
- (NSString*)stoneobs_fun_st_2floatAndMoney{
    return [NSString stringWithFormat:@"￥%0.2f",self.floatValue];
}

@end

